package com.sfx.messagequeue.mqServer.datacenter;

import com.sfx.messagequeue.MessageQueueApplication;
import com.sfx.messagequeue.mqServer.core.Binding;
import com.sfx.messagequeue.mqServer.core.Exchange;
import com.sfx.messagequeue.mqServer.core.ExchangeType;
import com.sfx.messagequeue.mqServer.core.MSGQueue;
import com.sfx.messagequeue.mqServer.mapper.MetaMapper;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created with IntelliJ IDEA.
 * Description: 通过这个类来完成整合数据库操作
 * User: sfx
 * Date: 2023-07-31
 * Time: 18:23
 */
@Slf4j
public class DataBaseManager {

    private MetaMapper metaMapper;

    //初始化数据库
    public void init() {
        //获取到MetaMapper对象-->初始化-->从Spring中得到
        metaMapper = MessageQueueApplication.context.getBean(MetaMapper.class);

        //判断数据库是否存在-->./data/meta.db这个文件存不存在
        //1.数据库存在
        File file = new File("./data/meta.db");
        if(!checkDbExists(file)) {
            File data = new File("./data");
            boolean ok = data.mkdirs();
            if(ok) {
                System.out.println("[DataBaseManger] 创建data目录成功");
            }else {
                System.out.println("[DataBaseManger] 创建data目录失败");
            }
            //数据库表不存在
            //1.1创建数据库表
            createDbTable();
            //1.2插入一些默认的数据
            insertDefaultData();
            System.out.println("[DataBaseManger :] 数据库初始化成功");
        }else {
            //2.数据库存在
            System.out.println("[DataBaseManger :] 数据库存在");
        }
    }

    //RabbitMQ 默认会创建一个交换机
    private void insertDefaultData() {
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setAutoDelete(false);
        exchange.setDurable(false);
        //保存到数据库-->默认创建一个交换机
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManger :] 创建交换机成功");
    }

    //创建数据库表
    private void createDbTable() {
        metaMapper.createBindingTable();
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        log.info("[DataBaseManger :] 创建数据表成功");
    }

    public void deleteDB() {
        //先删除文件
        File file = new File("./data/meta.db");
        boolean ok = file.delete();
        if(ok) {
            System.out.println("[DataBaseManger] 删除数据库文件成功");
        }else {
            System.out.println("[DataBaseManger] 删除数据库文件失败");
        }

        //在删除目录
        file = new File("./data");
        ok = file.delete();
        if(ok) {
            System.out.println("[DataBaseManger] 删除data目录成功");
        }else {
            System.out.println("[DataBaseManger] 删除data目录失败");
        }
    }

    //检查meta.db是否存在
    private boolean checkDbExists(File file) {
        return file.exists();
    }

    //增加交换机
    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }
    //删除交换机
    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }
    //查询所有的交换机
    public List<Exchange> selectAllExchange() {
        return metaMapper.selectAllExchange();
    }

    //增加队列
    public void insertQueue(MSGQueue msgQueue) {
        metaMapper.insertQueue(msgQueue);
    }
    //删除队列
    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }
    //查询所有的队列
    public List<MSGQueue> selectAllQueue() {
        return metaMapper.selectAllQueue();
    }

    //增加绑定
    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }
    //删除绑定
    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }
    //查询所有的绑定
    public List<Binding> selectAllBinding() {
        return metaMapper.selectAllBinding();
    }
}